import { Injectable } from '@nestjs/common';
import { PrismaService } from 'src/prisma/prisma.service';

@Injectable()
export class CalculateZoneService {
  constructor(private prisma: PrismaService) {}

  async calculate(xys: { x: number; y: number }[], zoneId: number) {
    // del db
    await this.prisma.calculateZone.deleteMany({ where: { zoneId } });

    const maxX = Math.max(...xys.map((xy) => xy.x));
    const maxY = Math.max(...xys.map((xy) => xy.y));
    const minX = Math.min(...xys.map((xy) => xy.x));
    const minY = Math.min(...xys.map((xy) => xy.y));

    // calc width and height
    // const startY = 2 * 10 * (Math.floor(minY / 2 / 10) - 1);
    // const endY = 2 * 10 * (Math.floor(maxY / 2 / 10) + 4);
    // const height = endY - startY;

    // const startX = 2 * 10 * Math.floor(minX / 2 / 10);
    // const endX = 2 * 10 * (Math.ceil(maxX / 2 / 10) + 4);
    // const width = endX - startX;

    return await this.prisma.calculateZone.create({
      data: {
        zoneId,
        maxX,
        maxY,
        minX,
        minY,
      },
    });
  }
}

// const calcCanvasAndHeight = async (
//   offsetWidth: number,
//   offsetHeight: number,
// ) => {
//   const minXY = section.value?.minXY;
//   const maxXY = section.value?.maxXY;

//   const [minx, miny] = JSON.parse(minXY!);
//   const [maxx, maxy] = JSON.parse(maxXY!);

//   const startY = 2 * 10 * (Math.floor(miny / 2 / 10) - 1);
//   let endY = 2 * 10 * (Math.floor(maxy / 2 / 10) + 4);
//   const caclHeight = endY - startY;

//   const startX = 2 * 10 * Math.floor(minx / 2 / 10);
//   let endX = 2 * 10 * (Math.ceil(maxx / 2 / 10) + 4);
//   const caclWidth = endX - startX;

//   const margin = 40;

//   // 获取宽度和长度
//   const width = Math.max(caclWidth + margin * 2, offsetWidth);
//   const height = Math.max(caclHeight + margin * 2, offsetHeight);
//   endX = 2 * 10 * Math.floor((width - margin * 2) / 2 / 10) + startX;
//   endY = 2 * 10 * Math.floor((height - margin * 2) / 2 / 10) + startY;

//   return {
//     width,
//     height,
//     startX,
//     endX,
//     startY,
//     endY,
//   };
// };
